package com.dingtalk.config;

import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.dingtalk.handler.GroupMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class DingTalkConfig {
    
    @Value("${dingtalk.app-key}")
    private String appKey;
    
    @Value("${dingtalk.app-secret}")
    private String appSecret;
    
    @Bean
    public GroupMessageHandler messageHandler() {
        return new GroupMessageHandler();
    }
    
    @Bean
    public com.aliyun.dingtalkoauth2_1_0.Client dingTalkClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }
    
    @Bean
    public String accessToken(com.aliyun.dingtalkoauth2_1_0.Client client) throws Exception {
        GetAccessTokenRequest request = new GetAccessTokenRequest()
            .setAppKey(appKey)
            .setAppSecret(appSecret);
        GetAccessTokenResponse response = client.getAccessToken(request);
        return response.body.accessToken;
    }
} 